<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CommentTable;
use App\Model\FieldTable;
use App\Model\InviteTeamTable;
use App\Model\LockSetTable;
use App\Model\PriceSetTable;
use App\Model\PriceSetTempTable;
use App\Model\StorefrontTable;
use App\Model\TeamMemberTable;
use App\Model\TeamTable;
use App\Model\TimeBlockTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Common\Fun;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use App\Repository\Sys\Sys;
use Illuminate\Support\Facades\DB;
use Psy\Util\Json;


class TeamController extends ApiController
{


    /**
     * @api {get} /api/team/game-list 应战列表
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id  店铺id 可选
     * @apiParam {Int} type  --
     * @apiParam {Date} day  必选2018-02-02
     * @apiParam {Time} time  11:00,起始时间
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "day": "01/01",
     * "time": "08:00-08:00",
     * "address": {
     * "storefront_name": "武汉-武昌",
     * "type_text": "5V5",
     * "field_id": "1",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "1"
     * },
     * "money": "1.00",
     * "field_info": {
     * "storefront_name": "武汉-武昌",
     * "type_text": "5V5",
     * "field_id": "1",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "1"
     * },
     * "team_info": {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1970-01-01",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "status": "1"
     * }
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getGameList()
    {
        $day = \Request::get('day');  //日期 必选
        $store_id = \Request::get('store_id');  //店面
        $time = \Request::get('time');  //字符串时间
        $type = \Request::get('type');  //
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        if (!$day) {
            return JsonRespose::fail('失败');
        }


        //  改成 原生sql  invite_team 表连接
        $where = 'status=2';
        $param = [];


        if ($store_id) {
            $where .= " and storefront_id=:storefront_id";
            $param['storefront_id'] = $store_id;
        }
        if ($type) {
            $where .= " and invite_team.type=:type";
            $param['type'] = $type;
        }


        $where .= " and game_day=:game_day";
        $param['game_day'] = Str::getDayTime($day);

        if ($time) {
            $where .= " and start_time=:start_time";
            $param['start_time'] = Str::timeTotime($time);
        }


        $count = \DB::select("select count(*) as c from invite_team LEFT  JOIN field ON field.id=invite_team.field_id WHERE  {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;

        $sql = "select *,invite_team.id as invite_id from invite_team LEFT  JOIN field ON field.id=invite_team.field_id WHERE  {$where} ORDER  BY invite_team.id DESC limit :offset,:limit";
        $field_list = \DB::select($sql, $param);

        $data = [];
        foreach ($field_list as $field) {

            $info = FieldTable::getFieldInfo($field->id);

            $data[] = [
                'id' => $field->invite_id,
                'day' => date('m/d', $field->game_day),
                'time' => date('H:i', $field->start_time) . '-' . date('H:i', $field->end_time).' 周'.Str::getWeek($field->game_day),
                'address' => FieldTable::getFieldInfo($field->id),
                'money' => $field->money,
                'field_info' => $info,
                'team_info' => TeamTable::getTeamInfo($field->team_id)  // 战队信息

            ];

        }


//        $data['my_team'] = TeamTable::getMyTeamList($uid);  //单独啦

        return JsonRespose::success([
            'count' => $count,
            'list' => $data,
            'page' => $page,
        ]);
    }


    /**
     * @api {get} /api/team/field-list 发布约战选择场地列表
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id  店铺id 可选
     * @apiParam {Int} field_id  场地id  可选
     * @apiParam {Date} day  必选2018-02-02
     * @apiParam {Time} time  11:00,起始时间
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "storefront_name": "武汉-武昌",
     * "type_text": "5V5",
     * "field_id": "1",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "1",
     * "count": 3
     * },
     * {
     * "storefront_name": "武汉-武昌",
     * "type_text": "6V6",
     * "field_id": "4",
     * "field_type": "6",
     * "storefront_id": "1",
     * "number": "4",
     * "count": 2
     * },
     * {
     * "storefront_name": "武汉-汉口",
     * "type_text": "5V5",
     * "field_id": "6",
     * "field_type": "5",
     * "storefront_id": "2",
     * "number": "1",
     * "count": 5
     * },
     * {
     * "storefront_name": "武汉-汉口",
     * "type_text": "6V6",
     * "field_id": "9",
     * "field_type": "6",
     * "storefront_id": "2",
     * "number": "4",
     * "count": 2
     * },
     * {
     * "storefront_name": "武汉-汉口",
     * "type_text": "7V7",
     * "field_id": "11",
     * "field_type": "7",
     * "storefront_id": "2",
     * "number": "6",
     * "count": 2
     * }
     * ]
     * }
     *
     */
    public function getFieldList()
    {
        $day = \Request::get('day');  //日期 必选
        $store_id = \Request::get('store_id');  //店面
        $time = \Request::get('start_time');  //字符串时间
        $type = \Request::get('type');  //场地类型

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        if (!$day) {
            return JsonRespose::fail('失败');
        }

        $day = Str::getDayTime($day);
        $db = StorefrontTable::ORM();

        if ($store_id) {
            $db->where('id', $store_id);
        }
        $store_res = $db->get();
        $data = [];

        foreach ($store_res as $s_v) {
            $field_tyes = FieldTable::ORM()->where('storefront_id', $s_v->id)->groupBy('type')->get();
            foreach ($field_tyes as $s_y) {

                if ($type) {
                    $field_res = FieldTable::ORM()->where('storefront_id', $s_v->id)->where('type', $type)->where('type', $s_y->type)->get();
                } else {
                    $field_res = FieldTable::ORM()->where('storefront_id', $s_v->id)->where('type', $s_y->type)->get();
                }

                // NVN的场地
                foreach ($field_res as $v) {
                    //查找是否有空的场地


                    if ($time) {
                        $time_list[] = Fun::getTimeSection(Str::timeTotime($time));
                    } else {
                        $time_list = TimeBlockTable::getTimeList();

                    }
//                    var_dump($time_list);

                    $f = false;
                    foreach ($time_list as $v_t) {
                        $res = TimeBlockTable::getBlockStatus($day, $v->id, $v_t['start_time'], $v_t['end_time']);
                        $status = FieldTable::getFieldStatus($res['order_id'], $res['lock']);
                        if ($status == 1) {
                            $f = true;
                        }
                    }

                    if ($f) {
                        if (isset($data[$s_v->id . '_' . $v->type])) {
                            $data[$s_v->id . '_' . $v->type]['count'] += 1;
                        } else {
                            $data[$s_v->id . '_' . $v->type]['info'] = FieldTable::getFieldInfo($v->id);
                            $data[$s_v->id . '_' . $v->type]['count'] = 1;
                        }
                    }


                }
            }
        }

        $list = [];
        foreach ($data as $v) {
            $v['info']['count'] = $v['count'];
            $list[] = $v['info'];
        }

        $data = [
            'count' => -1,
            'list' => $list,
            'page' => 1,

        ];
        return JsonRespose::success($data);
    }


    /**
     * @api {get} /api/team/field-time-list 发布约战选择场地时间
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} type  类型
     * @apiParam {Int} store_id  店铺id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "storefront_name": "武昌",
     * "type_text": "5V5",
     * "field_id": "1",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "1",
     * "list": [
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "09:00",
     * "end_time": "11:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "11:00",
     * "end_time": "13:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "13:00",
     * "end_time": "15:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "15:00",
     * "end_time": "17:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "17:00",
     * "end_time": "19:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "19:00",
     * "end_time": "21:00",
     * "field_id": "1",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "21:00",
     * "end_time": "23:00",
     * "field_id": "1",
     * "status": 1
     * }
     * ]
     * },
     * {
     * "storefront_name": "武昌",
     * "type_text": "5V5",
     * "field_id": "2",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "list": [
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "09:00",
     * "end_time": "11:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "11:00",
     * "end_time": "13:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "13:00",
     * "end_time": "15:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "15:00",
     * "end_time": "17:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "17:00",
     * "end_time": "19:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "19:00",
     * "end_time": "21:00",
     * "field_id": "2",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "21:00",
     * "end_time": "23:00",
     * "field_id": "2",
     * "status": 1
     * }
     * ]
     * },
     * {
     * "storefront_name": "武昌",
     * "type_text": "5V5",
     * "field_id": "3",
     * "field_type": "5",
     * "storefront_id": "1",
     * "number": "3",
     * "list": [
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "09:00",
     * "end_time": "11:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "11:00",
     * "end_time": "13:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "13:00",
     * "end_time": "15:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "15:00",
     * "end_time": "17:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "17:00",
     * "end_time": "19:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "19:00",
     * "end_time": "21:00",
     * "field_id": "3",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "1000.00",
     * "order_id": 0,
     * "start_time": "21:00",
     * "end_time": "23:00",
     * "field_id": "3",
     * "status": 1
     * }
     * ]
     * }
     * ]
     * }
     *
     */
    public function getFieldTimeList()
    {
        $type = \Request::get('type');
        $store_id = \Request::get('store_id');
        $day = \Request::get('day');

        $day=Str::getDayTime($day);
        $field_res = FieldTable::ORM()->where('storefront_id', $store_id)->where('type', $type)->get();

        $list = [];
        // NVN的场地
        foreach ($field_res as $v) {
            //查找是否有空的场地

            $time_list=TimeBlockTable::getFieldTimeList($v->id);

//            $time_list = ConstTable::DAY_TOTAL_BLOCK_LIST[$programs[$v->id]];
            foreach ($time_list as $v_t) {
                $data = TimeBlockTable::getBlockStatus($day, $v->id, $v_t['start_time'], $v_t['end_time']);
//                $data['price']=$data['price'];
                $data['field_id'] = $v->id;
                //$data['status'] = FieldTable::getFieldStatus($data['order_id'], $data['lock']);
               // if ($data['status'] == 1) {
                    $list[$v->type . '_' . $v->number][] = $data;
               // }
            }
        }


        $d = [];
        foreach ($list as $k => $v) {
            $info = FieldTable::getFieldInfo($v[0]['field_id']);
            $info['list'] = $v;

            $d[] = $info;
        }
        return JsonRespose::success($d);
    }


    /**
     * @api {get} /api/team/team-info 球队详情
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} team_id  战队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1970-01-01",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "status": "1",
     * "win": 90,
     * "count": "0",
     * "comment": [
     * {
     * "content": "123",
     * "content_num": "11"
     * }
     * ]
     * }
     * }
     *
     */
    public function getTeamInfo()
    {

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        $team_id = \Request::get('team_id'); //球队id


        $team = TeamTable::getTeamInfo($team_id);


        // 靠谱率
        $team->win = TeamTable::getWin($team_id);
        //完成比赛次数
        $count = \DB::select("select count(*) as c  from `order`  LEFT JOIN invite_team ON invite_team.id=`order`.object_id WHERE   pay_type>=2 and (order_type=1 or order_type=2) and pay_type = 2 and order.status = 2 and order_type !=3 and (team_id={$team->id} or join_team_id={$team->id})", [])[0]->c;

//        var_dump($count);die;
        $team->count = $count;


        $res = TeamMemberTable::ORM()->where('team_id', $team_id)->where('uid', $uid)->where('status', 1)->first();
        $team->is_join = $res ? 1 : 0;  //是否已加入  加入后人数加1
        $team->is_admin = 0;
        if ($uid == $team->admin_id) {
            $team->is_admin = 1;
        }

        //获取评论
        $comment = CommentTable::ORM()->where('team_id', $team->id)->limit(9)->get();
        $com = [];
        foreach ($comment as $v) {
            $com[] = [
                'content' => $v->content,
                'content_num' => $v->content_num
            ];
        }
        $team->comment = $com;
        return JsonRespose::success($team);

    }


    /**
     * @api {get} /api/team/game-info 应战详情
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} invite_id  约战id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "info_main_team": {
     * "id": "40",
     * "name": "秦天战队",
     * "img": "/uploads/image/20180226/1519627948399636.jpeg",
     * "create_time": "1488124800",
     * "colour": "20",
     * "desc": "我的球队",
     * "admin_id": "13",
     * "avg_age": "24",
     * "people_number": "1",
     * "status": "1"
     * },
     * "address": {
     * "storefront_name": "武汉-",
     * "type_text": "V",
     * "field_id": "2",
     * "field_type": "",
     * "storefront_id": "",
     * "number": ""
     * },
     * "invite_res": {
     * "id": "39",
     * "create_uid": "13",
     * "team_id": "40",
     * "join_team_id": "0",
     * "join_num": "1",
     * "avg_age": "24",
     * "colour": "20",
     * "desc": "我的球队",
     * "start_time": "10800",
     * "end_time": "18000",
     * "field_id": "2",
     * "money": "0.00",
     * "game_day": "1519574400",
     * "status": "2",
     * "dateline": "1519641107",
     * "slave_join_num": "",
     * "slave_avg_age": "",
     * "slave_age_colour": "",
     * "slave_desc": "",
     * "type": "5"
     * }
     * }
     * }
     *
     */
    public function getGameInfo()
    {

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        $invite_id = \Request::get('invite_id'); //约战id

        $invite__res = InviteTeamTable::ORM()->where('id', $invite_id)->first();

        $info_main_team = TeamTable::getTeamInfo($invite__res->team_id);

        $invite__res->game_day = date('m/d', $invite__res->game_day);

        $invite__res->start_time = date('H:i', $invite__res->start_time);
        $invite__res->end_time = date('H:i', $invite__res->end_time);


        return JsonRespose::success([
                'info_main_team' => $info_main_team,
                'address' => FieldTable::getFieldInfo($invite__res->field_id),
                'invite_res' => $invite__res
            ]
        );

    }

    /**
     * @api {get} /api/team/team-list 球队列表
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     * @apiParam {String} name --
     * @apiParam {String} phone --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1970-01-01",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "status": "1",
     * "phone": "13554239049",
     * "admin_name": "qinzhiyong",
     * "count": "0"
     * }
     * ],
     * "page": 1
     * }
     * }
     *
     */
    public function getTeamList()
    {


        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $name = \Request::get('name');  //球队名称
        $phone = '';

        return JsonRespose::done(TeamTable::TeamList($page, $limit, $name, $phone));

    }


    /**
     * @api {get} /api/team/my-team-list 我的球队列表
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "id": "1",
     * "team_id": "1",
     * "uid": "1",
     * "status": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "is_admin": 1
     * }
     * ]
     * }
     *
     */
    public function getMyTeamList()
    {

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }


        return JsonRespose::done(TeamTable::getMyTeamList($uid));

    }


    /**
     * @api {post} /api/team/team-member 球队成员列表
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} team_id  战队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "id": "1",
     * "team_id": "1",
     * "uid": "1",
     * "status": "1",
     * "phone": "13554239049",
     * "img": "",
     * "is_admin": 1
     * }
     * ]
     * }
     *
     */
    public function getTeamMember()
    {

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        $team_id = \Request::get('team_id'); //球队id
        $team = TeamTable::getTeamInfo($team_id);

        $res = DB::select("select team_member.*,user.phone,user.img,user.nickname from team_member LEFT JOIN `user` ON `user`.id =team_member.uid WHERE team_member.team_id={$team->id} and team_member.status=1", []);

        $data = [];
        foreach ($res as $v) {

            if ($v->uid == $team->admin_id) {
                $v->is_admin = 1;
            } else {
                $v->is_admin = 0;
            }
            $data[] = $v;
        }

        return JsonRespose::success([
            'count' => -1,
            'list' => $res,
            'page' => 1,
        ]);
    }

    /**
     * @api {post} /api/team/remove-member 踢出
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} uid  需要踢出的uid
     * @apiParam {Int} team_id  球队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     *
     */
    public function postRemoveMember()
    {
        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $member_uid = \Request::get('uid');
        $team_id = \Request::get('team_id'); //球队id
        $team = TeamTable::getTeamInfo($team_id);

        if ($uid != $team->admin_id) {
            return JsonRespose::fail('没有权限');
        } else {

            TeamMemberTable::ORM()->where('uid', $member_uid)->where('team_id', $team_id)->delete();
            TeamTable::ORM()->where('id', $team->id)->decrement('people_number', -1);

            TeamTable::updateCountNember($team->id);
            return JsonRespose::success();
        }
    }

    /**
     * @api {post} /api/team/out 退出
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} team_id  需要退出的球队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     *
     */
    public function postOut()
    {
        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $team_id = \Request::get('team_id'); //球队id


        $team = TeamTable::getTeamInfo($team_id);


        TeamMemberTable::ORM()->where('uid', $uid)->where('team_id', $team_id)->delete();
        TeamTable::ORM()->where('id', $team->id)->decrement('people_number', -1);

        TeamTable::updateCountNember($team_id);
        return JsonRespose::success();

    }


    /**
     * @api {post} /api/team/join-member 加入请求
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} uid  需要加入的uid
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     *
     */
    public function postJoinMember()
    {
        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $team_id = \Request::get('team_id'); //球队id

        $res = TeamMemberTable::ORM()->where('uid', $uid)->where('team_id', $team_id)->first();
        if ($res) {
            return JsonRespose::fail('已经提交过了');
        }
        TeamMemberTable::ORM()->where('uid', $uid)->insertGetId([
            'uid' => $uid,
            'team_id' => $team_id,
            'status' => 0
        ]);
        return JsonRespose::success();

    }


    /**
     * @api {get} /api/team/team-apply 申请管理
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} team_id  战队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "id": "1",
     * "team_id": "1",
     * "uid": "1",
     * "status": "1",
     * "phone": "13554239049",
     * "img": "",
     * "is_admin": 1
     * }
     * ]
     * }
     *
     */
    public function getTeamApply()
    {

        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }

        $team_id = \Request::get('team_id'); //球队id


        $team = TeamTable::getTeamInfo($team_id);

        if ($uid != $team->admin_id) {
            return JsonRespose::fail('没有权限');
        }

        $res = DB::select("select team_member.*,user.phone,user.img,user.nickname from team_member LEFT JOIN `user` ON `user`.id =team_member.uid WHERE team_member.team_id={$team->id} and team_member.status=0", []);

        $data = [];
        foreach ($res as $v) {
            if ($v->uid == $team->admin_id) {
                $v->is_admin = 1;
            } else {
                $v->is_admin = 0;
            }
            $data[] = $v;
        }
        return JsonRespose::success([
            'count' => -1,
            'list' => $res,
            'page' => 1,
        ]);
    }

    /**
     * @api {post} /api/team/join-member-pass 同意or忽略加入
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id  请求数据id
     * @apiParam {Int} action  1同意 2忽略
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     *
     */
    public function postJoinMemberPass()
    {
        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $id = \Request::get('id'); //数据id
        $action = \Request::get('action'); //球队id

        $res = TeamMemberTable::ORM()->where('id', $id)->first();

        TeamMemberTable::ORM()->where('id', $id)->update([
            'id' => $id,
            'status' => $action
        ]);

        if ($action == 1) {
            TeamTable::ORM()->where('id', $res->team_id)->decrement('people_number', 1);
        }

        TeamTable::updateCountNember($res->team_id);


        return JsonRespose::success();

    }


    /**
     * @api {post} /api/team/create-team 创建更新团队
     * @apiGroup Api-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id  可选,团队id
     * @apiParam {Int} img  -
     * @apiParam {Int} name -
     * @apiParam {Int} time  -
     * @apiParam {Int} colour -
     * @apiParam {Int} desc  -
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *{
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     *
     */
    public function postCreateTeam()
    {
        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $id = \Request::get('id'); //可选
        $img = \Request::get('img');
        $name = \Request::get('name');
        $time = \Request::get('create_time');
        $colour = \Request::get('colour');
        $desc = \Request::get('desc');
        $avg_age = \Request::get('avg_age');

        $res = TeamTable::ORM()->where('admin_id', $uid)->where('status', 1)->first();

        if ($res) {
            return JsonRespose::fail('您已创建过球队，无法创建');
        }


        if ($id) {
            $res = TeamTable::ORM()->where('id', $id)->update([
                'name' => $name,
                'img' => $img,
                'create_time' => Str::getDayTime($time),
                'colour' => $colour,
                'desc' => $desc,
//                'admin_id' => $uid,
                'avg_age' => $avg_age,
                'status' => 1
            ]);
        } else {
            $res = TeamTable::ORM()->insertGetId([
                'name' => $name,
                'img' => $img,
                'create_time' => Str::getDayTime($time),
                'colour' => $colour,
                'desc' => $desc,
                'admin_id' => $uid,
                'avg_age' => $avg_age,
                'people_number' => 1,
                'status' => 1
            ]);

            TeamMemberTable::ORM()->where('uid', $uid)->insertGetId([
                'uid' => $uid,
                'team_id' => $res,
                'status' => 1
            ]);


        }
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}